<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<!-- for循环的速度 -->
<script>
    const data = new Array(10000000).fill(0);
    function fun(key) {
        switch (key) {
            case 1:
                console.log('-----普通for循环-------');
                console.time('for');
                const result_1 = [];
                for (let i = 0; i < data.length; i++) {
                    result_1.push(data[i]);
                }
                console.timeEnd('for');
                console.log('-----执行完毕-------');
                break;
            case 2:
                console.log('-----for of-------');
                console.time('for_of');
                const result_2 = [];
                for (let item of data) {
                    result_2.push(item);
                }
                console.timeEnd('for_of');
                console.log('-----执行完毕-------');
                break;
            case 3:
                console.log('-----for in-------');
                console.time('for_in');
                const result_3 = [];
                for (let key in data) {
                    result_3.push(data[key]);
                }
                console.timeEnd('for_in');
                console.log('-----执行完毕-------');
                break;
            case 4:
                console.log('-----forEach-------');
                console.time('forEach');
                const result_4 = [];
                data.forEach((item) => {
                    result_4.push(item);
                });
                console.timeEnd('forEach');
                console.log('-----执行完毕-------');
                break;
            case 5:
                console.log('-----map-------');
                console.time('map');
                const result_5 = [];
                data.map((item) => {
                    result_5.push(item);
                });
                console.timeEnd('map');
                console.log('-----执行完毕-------');
                break;
            default:
                break;
        }
    }
    fun(1)
</script>

</html>